import java.util.Arrays;
import java.util.Scanner;

public class NGuessNumber {
    private int[] arr = new int[4];
    private int[] playerArr = new int[4];
    private int a = 0;
    private int b = 0;

    public static void main(String[] args) {
        NGuessNumber n = new NGuessNumber();
        while (true) {
            System.out.println("|----输入1开始游戏----|");
            System.out.println("|----输入0结束游戏----|\n");
            Scanner playerinput = new Scanner(System.in);
            n.generateAnswer();
            switch (playerinput.nextInt()){
                case 0:
                    System.out.println("----已退出游戏----\n");
                    System.exit(0);
                    break;
                case 1:
                    int cs = 0;
                    int sy = 7;

                    System.out.println("----已开始游戏(限制次数为7次)----\n");
                    n.generateAnswer();
                    while (true) {

                        n.getPlayerInput();
                        n.compareGuessAnswer();
                        if (n.isWin() == true) {
                            System.out.println("玩家获胜\n");
                            break;
                        } else {
                            cs++;
                            sy--;
                            if(cs==7){
                                System.out.println("7次到了，正确答案为"+ Arrays.toString(n.arr));
                                System.out.println("|----再来一局？------|");
                                break;
                            }
                            System.out.println("猜错了，在来一次！(还剩"+sy+"次)\n");
                        }
                    }
                    break;

            }
        }
    }

    public int[] generateAnswer() {

        int a, b, c, d;
        while(true){
            a = (int) (Math.random() * 9);
            b = (int) (Math.random() * 9);
            c = (int) (Math.random() * 9);
            d = (int) (Math.random() * 9);
            if (a != b && a != c && a != d && b != c && b != d) {
                arr = new int[]{a, b, c, d};
                break;
            }
        }
        return arr;
    }public int[] getPlayerInput() {

        System.out.println("请输入四个数字,");
        try {
            Scanner sca = new Scanner(System.in);

            for (int i = 0; i < playerArr.length; i++) {
                System.out.println("请输入0-9之间的第" + (i+1) + "个数字：");

                int pl = sca.nextInt();
                if (pl >= 10 || pl < 0) {
                    System.out.print("输入错误，");
                    i=i-1;
                }else {
                    playerArr[i] = pl;
                }
            }
        }catch (Exception e){
            System.out.println("输入错误");
        }



        return playerArr;
    }
    public int[] compareGuessAnswer() {
         a = 0;
         b = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == playerArr[i]) {
                a++;
            }

            for (int j = 0; j < arr.length; j++) {
                if (i != j) {
                    if (arr[i] == playerArr[j]) {
                        b++;
                    }
                }
            }
        }

        System.out.println(a+"A"+b+"B");
        return new int[]{a, b};
    }
    public int[] setArr(int[] r){
        arr = r;
        return r;
    }
    public boolean isWin() {
        if (a==4){
            System.out.println("你赢了！");
        }
        return a == 4;
    }

}
